<template>
  <div class="header-write-btns">
    <div v-show="isbtns"><BtnGroupTop  :showBtns="leftBtnList"  /></div>
    <div class="header-top-right">
      <div class="header-top-right-select" v-show="isSelect">
        <template   v-if="!secretLevel.hidden">
          <Select
              ref="secretLevelRef"   
              :label="this.$i18n('common.attachmentClassification.label.secretLevel')"
              width="90px"
              :options="secretLevel.options.filter(item=>item.value)"
              :placeholder="this.$i18n('common.pleaseSelect.label')"
              :defaultVal="secretLevel.value"
              @selectChangeVal="selectSecretLevelVal" />
          <div class="head-line"></div>
        </template>
        <Select
            :label="this.$i18n('common.importance.label')"
            :options="importantLevel.options"
            :placeholder="this.$i18n('common.importance.label')"
            width="90px"
            :defaultVal="importantLevel.value"
            @selectChangeVal="changeImportantLevel" />
        <div class="head-line"></div>
        <template v-if="showContentType">
          <Select
              ref="contentTypeRef"
              :label="this.$i18n('common.body.type.label')"
              :options="contentType.options.filter(item=>item.value)"
              :placeholder="this.$i18n('common.body.type.label')"
              :defaultVal="contentDefType"
              width="90px"
              @selectChangeVal="selectContentType" />
          <div class="head-line"></div>
        </template>
      </div>
      <BtnIconSelect v-show="isMore" :showBtns="rightBtnList" />
    </div>
  </div>
</template>

<script>
// 按钮button
import BtnGroupTop from '@/components/classic-layout/create/btn-group-top';
// 下拉框
import Select from '../select';
// 更多，三个点下拉框
import BtnIconSelect from '../btnIconSelect';
import { useBaseInfoStore } from '@/store/modules/base-info.js';
import {mapActions, mapState} from 'pinia';
import globalData, {BodyTypeEnums} from '@/plugins/data';
import {useNewButtonStore} from "@/store/modules/new-button";
import emitter from "@/utils/emitter";
import globalMethods from "@/plugins/methods";
export default {
  name: 'headerBtns',
  components: {
    BtnGroupTop,
    BtnIconSelect,
    Select
  },
  props: {
    /**
     * 展示部分
     * all 全部
     * btn 按钮部分
     * select 下拉部分
     * more 更多部分
     */
    // 展示部分
    btnsShowPart: {
      type: String,
      default: () => {
        return 'all';
      }
    },
    leftShowBtns:{
      type:Array,
      default:()=>['send','saveWaitSend']
    },
    rightShowBtns:{
      type:Array,
      default:()=>['callTemplates', 'saveAsTemplates', 'print','redirect']
    },
  },
  mounted () {
    emitter.subscribe('ContentLoaded', this.setMainContent);
  },
  beforeDestroy() {
    emitter.unsubscribe('ContentLoaded', this.setMainContent);
  },
  data() {
    return {
      /** 正文类型
       【content.contentType说明】
       HTML(10,"标准正文"
       FORM(20,"表单格式正文"
       CAP4FORM(21,"CAP4表单格式正文"
       TXT(30,"text正文"
       OfficeWord(41,
       OfficeExcel(42,
       WpsWord(43,
       WpsExcel(44,
       Pdf(45,
       Ofd(46,
       THIRD(90, 第三方正文
       **/
      // 重要程度默认值
      imtLevelDefVal: '1',
      // 密级默认值
      secretDefVal: null,
      // // 展示在更多icon下拉框里面的, 调用模板, 存为模板, 打印
      // leftShowBtns: ['send','saveWaitSend'],
      // MORE_ICON_SELECT: ['callTemplates', 'saveAsTemplates', 'print']
    };
  },
  computed: {
    ...mapState(useBaseInfoStore, ['baseInfo', 'formInfo','attachments']),
    ...mapState(useNewButtonStore, ['btnList', 'selectList', 'isSimple', 'isShowBodyTypeBtn']),
    // 正文类型
    contentType() {
      return (this.baseInfo && this.baseInfo.contentType) || { options: [] };
    },
    //左边发送按钮
    leftBtnList(){
      return this.btnList.filter(item=>{
        //固定发送和流程调整类
        return  this.leftShowBtns.includes(item.handle)||item.hasProcessChange===true;
      })
    },
    rightBtnList(){
      //固定打印等和磁贴
      return this.btnList.filter(item=>{
        //固定发送和流程调整类
        return  !this.leftBtnList.includes(item);
      })
    },
    // 重要程度
    importantLevel() {
      return this.formInfo.importantLevel;
    },
    // 密级管理
    secretLevel() {
      return (this.baseInfo && this.baseInfo.secretLevel) || { options: [] };
    },
    isbtns() {
      return this.btnsShowPart === 'all' || this.btnsShowPart === 'btn';
    },
    isSelect() {
      return this.btnsShowPart === 'all' || this.btnsShowPart === 'select';
    },
    isMore() {
      return this.btnsShowPart === 'all' || this.btnsShowPart === 'more';
    },
    showContentType(){
      const { isCap4Forward } = this.baseInfo.collaborationSubmitData.colMainData;
      return globalData.isStandard===true && isCap4Forward!=='true'
    },
    contentDefType(){
      return this.baseInfo?.businessData?.summary?.bodyType||'10'
    }
  },
  methods: {
    ...mapActions(useBaseInfoStore,['setContentType']),
    setMainContent() {
      let zwIframe = window._getZWIframe();
      if(this.baseInfo.zwIframe?.mainbody?.content && zwIframe?.$?.content) {
        let content = this.baseInfo.zwIframe.mainbody.content;
        zwIframe.$.content?.setContent(this.htmlDecode(content));
      }
    },
    htmlDecode(text) {
      let temp = document.createElement ("div");
      temp.innerHTML = text;
      let output = temp.innerText || temp.textContent;
      temp = null;
      return output;
    },
    selectContentType(val) {
      const zwIframe = window._getZWIframe();
      //加载项模式，屏蔽金格
      if (zwIframe.clientMixNet === 'disable' && zwIframe?.$.content?.switchContentType) {
        let chinaOs = globalMethods.isChinaOs();
        //国产系统且ofd类型
        let toJsp = chinaOs && [BodyTypeEnums.ofd.toString(), BodyTypeEnums.pdf.toString()].includes(`${val}`);
        if (toJsp) {
          this.toJspPage(val);
          return;
        }
        //最终确认后才修改值
        this.$refs.contentTypeRef.value = this.contentType.value;
        zwIframe?.$.content?.switchContentType(val, () => {
          this.setContentType(val);
          this.$refs.contentTypeRef.value = val;
          //调整尺寸
          emitter.publish('ContentLoaded', {})
        });
        return;
      }
      this.toJspPage(val);
    },
    toJspPage(val) {
      this.$confirm({
        title: this.$i18n('common.system.hint.label'),
        zIndex: 9999,
        // h 参数需保留，渲染需要该参数
        // eslint-disable-next-line no-unused-vars
        content: h => <div>{$.i18n('content.switchtype.message')}</div>,
        onOk: () => {
          this.contentType.value = val;
          let _href = globalData.ctxPath + "/collaboration/collaboration.do?showTab=true&recommendMenuId=newColl&method=newColl&fromNewItem=true&menuSummary=add&rescode=F01_newColl&chooseSplit=no";
          // &portalId=-65474635308972739
          _href += "&zwContentType=" + val;
          _href += "&isFullPage=true&_isModalDialog=true&isNew=true&moduleId=-1&moduleType=1&rightId=&officecanSaveLocal=true"; // 原功能默认值
          _href += "&officecanPrint="; // TODO 不知道干啥的
          if (this.formInfo.subject.value !== null && this.formInfo.subject.value !== undefined) {
            _href += "&subject=" + encodeURI(this.formInfo.subject.value);
          }
          _href += CsrfGuard.getUrlSurffix();
          console.log(_href)
          window.location = _href;
        },
        onCancel: () => {
          this.$refs.contentTypeRef.value = '10';
          this.contentType.value = '10';
        }
      });
    },
    selectSecretLevelVal(val) {
      if (this.secretLevel.value && Number(val) < Number(this.secretLevel.value)) {
        //有附件且调低密级
        if (this.attachments.atts && this.attachments.atts.length > 0) {
            //调低密级
          this.$confirm({
            title: $.i18n('system.reminder.title'),
            content: $.i18n('common.attachment.fileSecretLevel.lowerTip'),
            onCancel: () => {
              this.$refs.secretLevelRef.value = this.secretLevel.value;
            },
            onOk: () => {
              this.secretLevel.value = val;
              emitter.publish("clearAttachment", []);
            }
          });
          return;
        }
      }
      this.secretLevel.value = val;
    },
    changeImportantLevel(val) {
      this.importantLevel.value = val;
    }
  }
};
</script>

<style lang="scss" scoped>
.header-write-btns {
  display: flex;
  justify-content: space-between;
  align-items: center;
  .header-top-right {
    position: relative;
    display: flex;
    align-items: center;
    .header-top-right-select {
      display: flex;
      align-items: center;
    }
    .head-line {
      width: 1px;
      height: 16px;
      background-color: #e4e4e4;
      margin-right: 30px;
    }
  }
}
</style>
<style lang="scss">
.header-write-btns{
  .header-top-right{
    .ant-select-selection.ant-select-selection--single{
      background-color: transparent;
    }
  }
}

</style>
